package com.netease.homework.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.netease.homework.dao.ICommodityDao;
import com.netease.homework.meta.Commodity;
import com.netease.homework.service.ICommodityService;

@Component
@Qualifier("CommodityService")
public class CommodityServiceImpl implements ICommodityService {

	@Autowired
	private ICommodityDao commodityDao;

	@Override
	public Map<String, Object> getAllComm() throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		List<Commodity> list = commodityDao.getAllComm();

		if (list != null && list.size() > 0) {
			returnMap.put("value", list);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> getSelledComm() throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		List<Commodity> list = commodityDao.getSelledComm();

		if (list != null && list.size() > 0) {
			returnMap.put("value", list);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> getCommById(Integer id) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Commodity commodity = commodityDao.getCommById(id);

		if (commodity != null) {
			returnMap.put("value", commodity);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> addComm(String name, Double price, String picture, String summary, String description
			) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Commodity commodity = new Commodity();
		commodity.setName(name);
		commodity.setPrice(price);
		commodity.setPicture(picture);
		commodity.setSummary(summary);
		commodity.setDescription(description);
		commodity.setIsSelled(false);

		boolean result = commodityDao.addComm(commodity);

		if (result) {

			returnMap.put("value", commodity.getId());
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> editComm(Integer id, String name, Double price, String picture, String summary,
			String description, Boolean isSelled) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Commodity commodity = new Commodity(id, name, price, picture, summary, description, isSelled);

		int result = commodityDao.editComm(commodity);

		if (result>0) {
			returnMap.put("value",result);
			returnMap.put("success", true);
			returnMap.put("message", "修改成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	
	public Map<String, Object> deleteComm(Integer id){
		Map<String, Object> returnMap = new HashMap<String, Object>();

		
		int result = commodityDao.deleteComm(id);

		if (result>0) {
			returnMap.put("value",result);
			returnMap.put("success", true);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> getNoSelledComm() {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		List<Commodity> list = commodityDao.getNoSelledComm();

		if (list != null && list.size() > 0) {
			returnMap.put("value", list);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

}
